package com.example.mydy;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.mydy.model.Project;
import com.squareup.picasso.Picasso;

public class PublishProjectActivity extends AppCompatActivity {
    private EditText etProjectTitle, etProjectDescription, etProjectArticle, etProjectUnlockFee;
    private ImageView ivSelectedImage;
    private Uri selectedImageUri;
    private static final int PICK_IMAGE_REQUEST = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_project);

        initViews();
        setupToolbar();
    }

    private void initViews() {
        etProjectTitle = findViewById(R.id.et_project_title);
        etProjectDescription = findViewById(R.id.et_project_description);
        etProjectArticle = findViewById(R.id.et_project_article);
        etProjectUnlockFee = findViewById(R.id.et_project_unlock_fee);
        ivSelectedImage = findViewById(R.id.iv_selected_image);
        Button btnSelectImage = findViewById(R.id.btn_select_image);
        Button btnPublishProject = findViewById(R.id.btn_publish_project);

        btnSelectImage.setOnClickListener(v -> openImageChooser());
        btnPublishProject.setOnClickListener(v -> publishProject());
    }

    private void setupToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("发布项目");
        }
        toolbar.setNavigationOnClickListener(v -> onBackPressed());
    }

    private void openImageChooser() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "选择图片"), PICK_IMAGE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) {
            selectedImageUri = data.getData();
            if (selectedImageUri != null) {
                Picasso.get().load(selectedImageUri).into(ivSelectedImage);
                ivSelectedImage.setVisibility(ImageView.VISIBLE);
            }
        }
    }

    private void publishProject() {
        String title = etProjectTitle.getText().toString().trim();
        String description = etProjectDescription.getText().toString().trim();
        String article = etProjectArticle.getText().toString().trim();
        String unlockFeeStr = etProjectUnlockFee.getText().toString().trim();

        if (TextUtils.isEmpty(title) || TextUtils.isEmpty(description) || 
            TextUtils.isEmpty(article) || TextUtils.isEmpty(unlockFeeStr)) {
            Toast.makeText(this, "请填写所有字段", Toast.LENGTH_SHORT).show();
            return;
        }

        if (selectedImageUri == null) {
            Toast.makeText(this, "请选择一张项目图片", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            double unlockFee = Double.parseDouble(unlockFeeStr);
            Project project = new Project();
            project.setTitle(title);
            project.setDescription(description);
            project.setArticle(article);
            project.setReward(unlockFee);
            project.setImageUrl(selectedImageUri.toString());
            project.setStatus(Project.STATUS_ACTIVE);
            project.setCreatedAt(new java.util.Date());
            project.setCreatorId(1); // 假设当前用户ID为1

            // 保存项目并返回
            Intent resultIntent = new Intent();
            resultIntent.putExtra("project", project);
            setResult(RESULT_OK, resultIntent);
            finish();
            Toast.makeText(this, "项目发布成功", Toast.LENGTH_SHORT).show();
        } catch (NumberFormatException e) {
            Toast.makeText(this, "请输入有效的解锁费用", Toast.LENGTH_SHORT).show();
        }
    }
} 